<template>
  <Panel>
    <!-- 标题 -->
    <template #title>管理员信息</template>
    <!-- 内容 -->
    <template>
      <ul>
        <li>
          <span>管理员ID :</span>
          <p>{{ users.id }}</p>
        </li>
        <li>
          <span>账号 :</span>
          <p>{{ users.account }}</p>
        </li>
        <li>
          <span>用户组 :</span>
          <p>{{ users.userGroup }}</p>
        </li>
        <li>
          <span>创建时间 :</span>
          <p>{{ users.ctime | dateFormat }}</p>
        </li>
        <li>
          <span>管理员头像 :</span>
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1:5000/users/avatar_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </li>
        <li>
          <el-button @click="headerImg" type="primary" round
            >确定修改</el-button
          >
        </li>
      </ul>
    </template>
  </Panel>
</template>

<script>
import loca from '@/utils/loca.js'
import { header } from '../../api/user'
export default {
  data() {
    return {
      users: {},
      imageUrl: ''
    }
  },
  created() {
    this.users = JSON.parse(loca.get('user'))
  },
  methods: {
    handleAvatarSuccess(res, file) {
      loca.set('imgUrls', res.imgUrl)
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 头像渲染
    async headerImg() {
      loca.get('imgUrls')
      console.log(typeof loca.get('imgUrls'))
      const { code, msg } = await header({
        imgUrl: loca.get('imgUrls')
      })
      // console.log(data)
      if (code === 0) {
        this.$message({
          type: 'success',
          message: msg
        })
        location.reload()
      } else {
        this.$message({
          type: 'error',
          message: msg
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>
li {
  display: flex;
  margin: 20px 0;
  line-height: 50px;
  span {
    width: 120px;
    margin-right: 20px;
  }
  .el-button {
    margin: 10px auto;
  }
  // less 的穿刺写法
  /deep/.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
}
</style>
